package jlts.front.ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 * @ClassName ExamFrame
 * @Description TODO
 * @Author Zxeao
 * @Date 2021/12/21 上午11:31
 * @Version V1.0
 */
public class ExamFrame extends JFrame {
    private JLabel titleLabel=new JLabel();
    //考试信息
    private JLabel infoLabel=new JLabel();
    //题号
    private JLabel questionNumLabel=new JLabel();
    //倒计时
    private JLabel timeLabel=new JLabel();
    //题干
    private JTextArea questionArea=new JTextArea();
    //选项
    private JCheckBox[] options=new JCheckBox[4];
    //上一题
    private JButton preBtn=new JButton();
    //下一题
    private JButton nextBtn=new JButton();
    //交卷按钮
    private JButton submitBtn=new JButton();

    private JPanel contentPanel=new JPanel();
    private JPanel northPanel=new JPanel();
    private JPanel centerPanel=new JPanel();
    private JPanel infoPanel=new JPanel();
    private JScrollPane questionAreaPanel=new JScrollPane();
    private JPanel southPanel=new JPanel();
    private JPanel optionAndBtnPanel=new JPanel();
    private JPanel optionPanel=new JPanel();
    private JPanel btnPanel=new JPanel();

    public ExamFrame() {
        this.setSize(550, 450);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setLocationRelativeTo(null);
        initContentPanel();
        this.add(contentPanel);
        this.setResizable(false);
        this.setVisible(false);
    }

    private void initContentPanel() {
        //设置面板布局
        contentPanel.setLayout(new BorderLayout());
        //设置面板边框
        contentPanel.setBorder(new EmptyBorder(8, 8, 8, 8));
        //初始化组件
        initNorthPanel();
        initCenterPanel();
        initSouthPanel();
        //添加组件
        contentPanel.add(northPanel,BorderLayout.NORTH);
        contentPanel.add(centerPanel,BorderLayout.CENTER);
        contentPanel.add(southPanel,BorderLayout.SOUTH);
    }

    private void initNorthPanel() {
        northPanel.setLayout(new BorderLayout());
        northPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
        URL url=getClass().getResource("img/exam_title.png");
        ImageIcon icon=new ImageIcon(url);
        titleLabel.setIcon(icon);;
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        northPanel.add(titleLabel,BorderLayout.CENTER);
    }

    private void initCenterPanel() {
        centerPanel.setLayout(new BorderLayout());
        centerPanel.setBorder(new EmptyBorder(8, 8, 8, 8));
        initInfoPanel();
        initAreaPanel();
        centerPanel.add(infoPanel,BorderLayout.NORTH);
        centerPanel.add(questionAreaPanel,BorderLayout.CENTER);
    }

    private void initInfoPanel() {
        infoPanel.setLayout(new FlowLayout());
        infoPanel.setBorder(new EmptyBorder(8, 8, 8, 8));
        infoLabel.setText("考试科目：驾考一 考试时间120分钟 题目数量 20");
        infoLabel.setHorizontalAlignment(JLabel.CENTER);
        infoPanel.add(infoLabel);
    }

    private void initAreaPanel() {
        questionAreaPanel.setBorder(new TitledBorder("题目"));
        questionArea.setColumns(50);
        questionArea.setLineWrap(true);
        // 允许折行显示
        questionArea.setEditable(false);
        // 不能够编辑内容
        questionArea.setText("以下选项是哪个对 A我对 B他对 C你对 D 都不对");
        //必须放到JScrollPane的视图范围内(Viewport)
        questionAreaPanel.getViewport().add(questionArea);
    }

    private void initSouthPanel() {
        southPanel.setLayout(new BorderLayout());
        southPanel.setBorder(new EmptyBorder(6, 6, 6, 6));
        timeLabel.setText("还剩12分钟");
        timeLabel.setHorizontalAlignment(JLabel.RIGHT);
        southPanel.add(timeLabel,BorderLayout.EAST);

        questionNumLabel.setText("20题中的第1题");
        questionNumLabel.setHorizontalAlignment(JLabel.RIGHT);

        southPanel.add(questionNumLabel,BorderLayout.WEST);
        initOptionAndBtnPanel();
        southPanel.add(optionAndBtnPanel,BorderLayout.CENTER);
    }

    private void initOptionAndBtnPanel() {
        optionAndBtnPanel.setLayout(new BorderLayout());
        optionAndBtnPanel.setBorder(new EmptyBorder(6, 6, 6, 6));
        initOptionPanel();
        initBtnPanel();
        optionAndBtnPanel.add(optionPanel,BorderLayout.NORTH);
        optionAndBtnPanel.add(btnPanel,BorderLayout.CENTER);
    }

    private void initOptionPanel() {
        for(int i=0;i<options.length;i++){
            JCheckBox option=new JCheckBox();
            option.setText((char)('A'+i)+"");
            options[i]=option;
            optionPanel.add(option);
        }
    }

    private void initBtnPanel() {
        preBtn.setText("上一题");
        nextBtn.setText("下一题");
        submitBtn.setText("交卷");

        btnPanel.add(preBtn);
        btnPanel.add(nextBtn);
        btnPanel.add(submitBtn);

        preBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.print("上一题，该功能暂未开发");
            }
        });
        nextBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.print("下一题，该功能暂未开发");
            }
        });
        submitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.print("提交，该功能暂未开发");
            }
        });

    }

}
